<template>
  <div class="app-main-container">
    <!-- <div class="app-main-title">会员公告</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="toNoticeAdd(0)">新增</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" label="标题" prop="title" min-width="200">
        <template slot-scope="scope">
          <span class="han1" :title="scope.row.title">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="内容"
        prop="content"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="关联场馆"
        prop="venueName"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        min-width="200"
      />
      <el-table-column
        align="center"
        label="发布人员"
        prop="publishName"
        min-width="200"
      />
      <el-table-column
        align="center"
        label="发布时间"
        prop="publishTime"
        min-width="200"
      />
      <el-table-column align="center" label="状态" min-width="140" fixed="right">
        <template slot-scope="scope">
          {{ scope.row.status == "0" ? "待发布" : "已发布" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="scope.row.status == '0'"
              v-preventReClick
              type="text"
              @click="toNoticePublish(scope.row.id)"
            >发布</el-button>
            <el-button
              v-if="scope.row.status == '0'"
              type="text"
              @click="toNoticeAdd('1', scope.row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              style="color: red"
              @click="toNoticeDelete(scope.row.id)"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :hide-on-single-page="true"
        :current-page.sync="cur_page"
        @current-change="getList"
      />
    </div> -->
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <!-- 新增 编辑 公告 -->
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      :title="addEditType ? '编辑公告' : '新增公告'"
      :visible.sync="showAddNotice"
      @closed="cancelAddNotice"
    >
      <el-form
        ref="ruleForm"
        :model="formAddNotice"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="formAddNotice.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="formAddNotice.content" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item label="关联场馆" prop="recipientsIds">
          <el-checkbox-group v-model="formAddNotice.recipientsIds">
            <el-checkbox
              v-for="item in relation"
              :key="item.id"
              :label="item.id"
              :value="item.id"
            >{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button v-preventReClick @click="cancelAddNotice">取消</el-button>
        <el-button
          v-preventReClick
          type="success"
          @click="confirmAddNotice('0')"
        >保存</el-button>
        <el-button
          v-preventReClick
          :loading="publishLoading"
          type="primary"
          @click="confirmAddNotice('1')"
        >发布</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { noticeList, noticeAdd, noticePublish, noticeEdit, noticeParam, noticeDelete, venueList } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeVenue',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      modalDialog: this.$store.state.app.dialog,
      publishLoading: false,
      listLoading: false,
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {
        type: '2'
      },
      relation: [],
      addEditType: 0,
      showAddNotice: false,
      formAddNotice: {
        title: '',
        content: '',
        recipientsIds: [],
        type: '2'
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        recipientsIds: [
          { type: 'array', required: true, message: '请选择关联场馆', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList(1)
    this.getVenueList()
  },
  methods: {
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      }).then(res => {
        if (res.errorCode !== '000000') return
        this.relation = res.recordList
        const arr = res.recordList
        arr.forEach(e => {
          e.label = e.name
          e.value = e.id
        })
        this.queryFormConfig.formItems[1].options = arr
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      noticeList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = { type: '2' }
      this.getList(1)
    },
    // 新增/编辑消息
    toNoticeAdd(type, id) {
      this.addEditType = type
      this.showAddNotice = true
      if (id) {
        noticeParam({ param: { id }}).then(res => {
          if (res.errorCode === '000000') {
            res.record.recipientsIds = res.record.recipientsList
            res.record.type = '2'
            this.formAddNotice = res.record
          }
        })
      }
    },
    // 取消新增/编辑
    cancelAddNotice() {
      this.showAddNotice = false
      this.formAddNotice = {
        title: '',
        content: '',
        recipientsIds: [],
        type: '2'
      }
    },
    // 确认新增/编辑
    confirmAddNotice(isPublish) {
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const { addEditType } = this
          const param = { ...this.formAddNotice }
          param.isPublish = isPublish
          that.publishLoading = true
          if (addEditType) {
            noticeEdit({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              this.getList(1)
              this.showAddNotice = false
            })
          } else {
            noticeAdd({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              this.getList(1)
              this.showAddNotice = false
            })
          }
        } else {
          that.publishLoading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    // 发布通知
    toNoticePublish(id) {
      noticePublish({ param: { id }}).then(res => {
        if (res.errorCode !== '000000') return
        this.getList(1)
      })
    },
    // 删除通知
    toNoticeDelete(id) {
      this.$confirm('此操作将删除该通知, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          noticeDelete({ param: { id }}).then(ress => {
            if (ress.errorCode === '000000') {
              this.getList(1)
            }
          })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    }
  }
}
</script>
<style scoped>
.han1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
